//
//  JSMLeavingHotDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingHotDetailsViewController.h"
#import "JSMLeavingHotDetailsCollectionCell.h"
#import "JSMLeavingHotDetailsCollectionReusableView.h"
#import "JSMLeavingHotDetailsModel.h"
#import "JSMLeavingHotDetailsDaysModel.h"
#import "JSMLeavingScrollModel.h"
@interface JSMLeavingHotDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) BaseCollectionView *leavingHotDetialsCollectionView;
@property(nonatomic, strong) NSMutableArray *leavingHotDetasilsMarr;
@property(nonatomic, strong) JSMLeavingHotDetailsModel *leavingHotDetailsModel;
@property(nonatomic, strong) NSMutableArray *leavingHotDetailsDaysMarr;

@end

@implementation JSMLeavingHotDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createLeavingHotDetailsCollectionView];
    [self createData];
    
}

- (void)createLeavingHotDetailsCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH - 30, 0);
    self.leavingHotDetialsCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.leavingHotDetialsCollectionView];
    self.leavingHotDetialsCollectionView.dataSource = self;
    self.leavingHotDetialsCollectionView.delegate = self;
    [self.leavingHotDetialsCollectionView registerClass:[JSMLeavingHotDetailsCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    flowLayout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 30);
    [self.leavingHotDetialsCollectionView registerClass:[JSMLeavingHotDetailsCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseableView"];
    
}

- (void)createData {
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", self.leavingHotModel.urlId];
    [AFNetworkingTool getUrl:url hudSuperView:self.view result:JSON success:^(id result) {
        if (result != nil) {
            self.leavingHotDetailsModel = [[JSMLeavingHotDetailsModel alloc] init];
            [self.leavingHotDetailsModel setValuesForKeysWithDictionary:result];
            self.leavingHotDetailsDaysMarr = [NSMutableArray array];
            self.leavingHotDetailsDaysMarr = self.leavingHotDetailsModel.daysMarr;
            if (self.leavingHotDetailsDaysMarr.count > 0) {
                [self.leavingHotDetialsCollectionView reloadData];
            }
        }
    } failure:^(NSError *error) {
    }];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        JSMLeavingHotDetailsCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"reuseableView" forIndexPath:indexPath];
        headerView.titleLabel.text = [NSString stringWithFormat:@"第%ld天", indexPath.section + 1];
        return headerView;
    }
    return nil;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[self.leavingHotDetailsDaysMarr[section] waypointsMarr] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMLeavingHotDetailsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[[self.leavingHotDetailsDaysMarr[indexPath.section] waypointsMarr][indexPath.row] photo]] placeholderImage:PlaceHolderImage];
    cell.mainLabel.text = [[self.leavingHotDetailsDaysMarr[indexPath.section] waypointsMarr][indexPath.row] textStr];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.leavingHotDetailsDaysMarr.count;
}
//  自定义cell高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [[[self.leavingHotDetailsDaysMarr[indexPath.section] waypointsMarr][indexPath.row]textStr] boundingRectWithSize:CGSizeMake(SCREENWIDTH - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return CGSizeMake(SCREENWIDTH - 30, rect.size.height + 205);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
